package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.activity.ActivityParam;
import cn.ulc.socket.admin.model.param.activityGoods.ActivityGoodsParam;
import cn.ulc.socket.admin.service.ActivityGoodsServ;
import cn.ulc.socket.admin.service.marketing.ActivityServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

/*
*
* gen controller 2019-06-16
*/
@Validated
@RestController
@RequestMapping("/activity")
public class ActivityCtrl {
  private static final Log log = LogFactory.get();

    @Autowired
    ActivityServ activityServ;
    @Autowired
    ActivityGoodsServ activityGoodsServ;

    @GetMapping("/goods/list")
    public Result findGoodsList(Long activityId,String key) {
        return Result.success(activityGoodsServ.findList(activityId,key));
    }

    @PostMapping("/goods/add")
    public Result addGoods(@Validated @RequestBody ActivityGoodsParam param, BindingResult result) {
      if(result.hasErrors()) {
        log.error("保存活动商品详情参数错误",result.getFieldError().getDefaultMessage());
        return Result.fail(result.getFieldError().getDefaultMessage());
      }
        return Result.success(activityGoodsServ.saveAll(param));
    }


    @DeleteMapping("/goods/{id}")
    public Result deletedGoods(@PathVariable @Min(1) Long id) {
        return Result.success(activityGoodsServ.deleteOne(id));
    }

    @GetMapping
    public Result findList(String key,String startDate,String endDate, PaginationParam pager) {
        return Result.success(activityServ.findList(key,startDate,endDate, pager));
    }

    @GetMapping(value = "/{id}")
    public Result findDetail (@PathVariable @Min(1) Long id) {
        return Result.success(activityServ.findDetail(id));
    }

  @PostMapping
  public Result save(@Validated @RequestBody ActivityParam param, BindingResult result){
    if(result.hasErrors()) {
      log.error("保存活动详情参数错误",result.getFieldError().getDefaultMessage());
      return Result.fail(result.getFieldError().getDefaultMessage());
    }
    return Result.success(activityServ.saveOne(param));
  }
  @PutMapping("/{id}")
  public Result update(@PathVariable("id") Long id, @RequestBody ActivityParam param, BindingResult result){
    if(result.hasErrors()) {
      log.error("保存活动详情参数错误",result.getFieldError().getDefaultMessage());
      return Result.fail(result.getFieldError().getDefaultMessage());
    }
    param.setId(id);
    return Result.success(activityServ.saveOne(param));
  }
  @DeleteMapping("/{id}")
  public Result deleted(@PathVariable("id") Long id){
    return Result.success(activityServ.deleteOne(id));
  }

  @PutMapping("/changeStatus/{id}")
  public Result changeStatus(@PathVariable("id") Long id, AvailableStatus status){
    return Result.success(activityServ.changeStatus(id,status));
  }
}
